package com.yc.rh.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.yc.rh.utils.SessionAttributeNames;

import com.yc.rh.utils.ReflectUtils;
import com.yc.rh.biz.ICommentsInfoBiz;
import com.yc.rh.biz.INewartInfoBiz;
import com.yc.rh.biz.IUserInfoBiz;
import com.yc.rh.biz.impl.CommentsInfoBizImpl;
import com.yc.rh.biz.impl.NewartInfoBizImpl;
import com.yc.rh.biz.impl.UserInfoBizImpl;
import com.yc.rh.entities.CommentsInfo;
import com.yc.rh.entities.JsonObject;
import com.yc.rh.entities.NewartInfo;
import com.yc.rh.entities.UserInfo;

@SuppressWarnings("unchecked")
public class CommentsInfoAction extends ActionSupport implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	private HttpSession session;
	private HttpServletRequest request;
	@SuppressWarnings("rawtypes")
	private JsonObject jsonObject;
	private ICommentsInfoBiz commentsInfoBiz=new CommentsInfoBizImpl();
	private INewartInfoBiz newartInfoBiz=new NewartInfoBizImpl();
	private IUserInfoBiz userInfoBiz=new UserInfoBizImpl();
	
	private CommentsInfo commentsInfo;
	private UserInfo userInfo;

	public ICommentsInfoBiz getCommentsInfoBiz() {
		return commentsInfoBiz;
	}

	public void setCommentsInfoBiz(ICommentsInfoBiz commentsInfoBiz) {
		this.commentsInfoBiz = commentsInfoBiz;
	}

	public INewartInfoBiz getNewartInfoBiz() {
		return newartInfoBiz;
	}

	public void setNewartInfoBiz(INewartInfoBiz newartInfoBiz) {
		this.newartInfoBiz = newartInfoBiz;
	}

	public IUserInfoBiz getUserInfoBiz() {
		return userInfoBiz;
	}

	public void setUserInfoBiz(IUserInfoBiz userInfoBiz) {
		this.userInfoBiz = userInfoBiz;
	}

	@SuppressWarnings("rawtypes")
	public JsonObject getJsonObject() {
		return jsonObject;
	}

	@SuppressWarnings("rawtypes")
	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public CommentsInfo getCommentsInfo() {
		return commentsInfo;
	}

	public void setCommentsInfo(CommentsInfo commentsInfo) {
		this.commentsInfo = commentsInfo;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request=arg0;
		session=request.getSession();
	}
	
	public String getUserInfoAndNewartInfo(){
		List<UserInfo> list1=userInfoBiz.findUserInfoByPage(null);
		List<NewartInfo> list2=newartInfoBiz.find(null, null);
		List<Object> list=new ArrayList<Object>();
		list.addAll(list1);
		list.addAll(list2);
		jsonObject.setRows(list);
		return "success";
	}
	
	public String CommentData() {
		String pic_naname=request.getParameter("pic_naname");
		int naid=newartInfoBiz.getNaid(pic_naname);
		session.setAttribute(SessionAttributeNames.COMMENTDATA, naid);
		jsonObject.setResult(1);
		return "success";
	}
	
	/**
	 * 页面评论的数据准备
	 * @param request
	 * @param response
	 */
	public String CommentDatas() {
		int naid= (Integer) session.getAttribute(SessionAttributeNames.COMMENTDATA);
		int pageNo;
		if(request.getParameter("pageNo")==null){
			pageNo=1;
		}else{
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}
		
		int pageSize=5;	//默认每页显示5条
		session.setAttribute("pageSize", pageSize);	
		
		int totalSize=commentsInfoBiz.getTotal(naid);  //总记录数
		int totalPage=(totalSize+pageSize-1)/pageSize;  //总页数
		
		List<CommentsInfo> list=commentsInfoBiz.find(pageNo,pageSize,naid);
		for(int i=0;i<list.size();i++){
			int usid=list.get(i).getUsid();
			String uname=userInfoBiz.findUnameByUsid(usid);
			list.get(i).setText2(uname);
		}
		
		System.out.println(list);
		jsonObject.setRows(list);
		jsonObject.setTotalSize(totalSize);
		jsonObject.setTotalPage(totalPage);
		jsonObject.setPageNo(pageNo);
		return "success";
	}

	/**
	 * 添加评论
	 * @param request
	 * @param response
	 */
	public String addComments() {
//		String commentContent=request.getParameter("commentContent");
//		UserInfo uf=new UserInfo(1000, "陆超磊", "123456", "714468985@qq.com", "18274707052", "浙江", "嘉兴市", "平湖市", "2015-1-1", 1, "", "");
//		session.setAttribute(SessionAttributeNames.CURRENTUSERINFO, uf);
		
		int pageSize=(Integer) session.getAttribute("pageSize");
		
		Object obj=session.getAttribute(SessionAttributeNames.CURRENTUSERINFO);
		if(obj!=null){
			int naid=(Integer) session.getAttribute(SessionAttributeNames.COMMENTDATA);
			UserInfo uf=(UserInfo) obj;
			CommentsInfo commentsInfo=(CommentsInfo)ReflectUtils.regInfo(request, CommentsInfo.class);
			commentsInfo.setNaid(naid);
			commentsInfoBiz.addCommentsInfo(uf,commentsInfo);
			
			jsonObject.setTotal(commentsInfoBiz.getTotal(naid));
			jsonObject.setPageSize(pageSize);
		}else{
			jsonObject.setTotal(-1);
		}
		return "success";
	}
	
	public String updateCommentsInfo() {
		return "success";
	}
	
	public String delCommentsInfo() {
		String csids=request.getParameter("csids");
		jsonObject.setResult(commentsInfoBiz.delComments(csids));
		return "success";
	}
	
	/**
	 * 后台查评论数据
	 * @param request
	 * @param response
	 */
	public String getComments() {
		int page=Integer.parseInt(request.getParameter("page"));
		int rows=Integer.parseInt(request.getParameter("rows"));
		List<CommentsInfo> list=commentsInfoBiz.find(page, rows,0);
		jsonObject.setRows(list);
		jsonObject.setTotal(commentsInfoBiz.getTotal(0));
		return "success";
	}
}

